package cn.sc.summer.calculate.math;

import cn.sc.summer.constant.util.AssertUtil;
import cn.sc.summer.exception.enums.CommonExceptionEnum;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 类名：计算工具类
 *
 * @author a-xin
 * @date 2024/1/29 13:20
 */
@Component
public class MathUtil {

    /**
     * 根据数据权重获取最终分数
     *
     * @param data    基础数据
     * @param weights 权重数据
     * @return 最终分数
     */
    public static BigDecimal weightedSum(Map<String, BigDecimal> data, Map<String, BigDecimal> weights) {
        AtomicReference<BigDecimal> weightedSum = new AtomicReference<>(BigDecimal.ZERO);
        for (Map.Entry<String, BigDecimal> entry : data.entrySet()) {
            String key = entry.getKey();
            BigDecimal value = entry.getValue();
            CommonExceptionEnum.NORMAL_EXCEPTION.exceptionIf(!weights.containsKey(key), () -> {
                BigDecimal weight = weights.get(key);
                weightedSum.updateAndGet(sum -> sum.add(value.multiply(weight)));
            }, "No weight specified for data: " + key);
        }
        return weightedSum.get();
    }

    /**
     * 计算一组数据的方差
     *
     * @param data 数据集合
     * @return 方差
     */
    public static BigDecimal variance(List<BigDecimal> data) {

        BigDecimal average = average(data);
        BigDecimal sumSquaredDiff = BigDecimal.ZERO;
        for (BigDecimal num : data) {
            BigDecimal diff = num.subtract(average);
            sumSquaredDiff = sumSquaredDiff.add(diff.multiply(diff));
        }

        return sumSquaredDiff.divide(new BigDecimal(data.size()), 4, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算一组数据的平均数
     *
     * @param data 数据集合
     * @return 平均数
     */
    public static BigDecimal average(List<BigDecimal> data) {
        return data.stream().reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal(data.size()), 4, RoundingMode.HALF_UP);
    }

    /**
     * 计算一组数据的权重
     * @param data 数据集合
     * @return 数据权重
     */
    public static Map<Double, Double> calculateWeights(List<Double> data) {
        Map<Double, Double> weightsMap = new HashMap<>();
        BigDecimal total = BigDecimal.valueOf(data.stream().reduce(Double::sum).get());
        for (Double original : data) {
            AssertUtil.functionIf(total.compareTo(BigDecimal.ZERO) > 0, () -> {
                weightsMap.put(original, new BigDecimal(original).divide(total, 4, RoundingMode.HALF_UP).doubleValue());
            });
        }
        return weightsMap;
    }

    /**
     * 计算一组数据的标准差
     *
     * @param data 数据集合
     * @return 标准差
     */
    public static BigDecimal standardDeviation(List<BigDecimal> data) {

        BigDecimal sum = BigDecimal.ZERO;
        for (BigDecimal num : data) {
            sum = sum.add(num);
        }
        BigDecimal mean = sum.divide(new BigDecimal(data.size()), MathContext.DECIMAL128);

        BigDecimal sumSquaredDiff = BigDecimal.ZERO;
        for (BigDecimal num : data) {
            BigDecimal diff = num.subtract(mean);
            sumSquaredDiff = sumSquaredDiff.add(diff.multiply(diff));
        }

        BigDecimal variance = sumSquaredDiff.divide(new BigDecimal(data.size()), MathContext.DECIMAL128);

        return new BigDecimal(Math.sqrt(variance.doubleValue()), MathContext.DECIMAL128);
    }

}
